package controller;

import java.io.IOException;

import model.Game;
import model.Video;

import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Textbox;

import util.FileUtils;

import admin.dao.GameDAO;
import admin.dao.VideoDAO;

public class AddGameCtrl extends SelectorComposer<Component>{
	private static final long serialVersionUID = 1L;

	@Wire
    private Textbox txtGamename;
	
	private String filePatch;
	
	
	@Listen("onUpload = #btUpload")
    public void upload(UploadEvent event){
		Media media = event.getMedia();
		try {
			filePatch = FileUtils.saveFileFromMedia(media, null,"game");
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
	
	@Listen("onClick = #btSubmit")
    public void submit(){
		GameDAO.addGame(new Game(txtGamename.getValue(), "admin", "game/"+filePatch));
		Executions.getCurrent().sendRedirect("game_layout.jsp");
    }
}
